//
//  ReservationViewController.m
//  homeland
//
//  Created by SunShine on 13-10-30.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "ReservationViewController.h"
#define INPUT_VIEW_OFFSET 50.0

@interface ReservationViewController ()
@property (strong, nonatomic) NSDate *pickedDate;
@property (strong, nonatomic) NSDate *pickedBeginTime;
@property (strong, nonatomic) ReservationService *dataService;
@end

@implementation ReservationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"政务服务中心";
    self.dataService = [ReservationService new];
    _dataService.delegate = self;
    _bussinessNameField.text = _listItem.businessName;
    _unitNameField.text = _listItem.unitName;
    _nameField.inputAccessoryView = [self getInputAccessoryView];
    _nameField.tag = 1;
    _phoneField.inputAccessoryView = [self getInputAccessoryView];
    _phoneField.tag = 2;
    _cardField.inputAccessoryView = [self getInputAccessoryView];
    _cardField.tag = 3;
    [_scrollView addSubview:_formPanelView];
    _scrollView.contentSize = _formPanelView.frame.size;
    //初始化选择器
    _datePicker = [[DatePicker alloc]initWithFrame:CGRectZero];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Button
- (IBAction)commitButtonAction:(id)sender
{
    NSString *name = _nameField.text;
    NSString *phone = _phoneField.text;
    NSString *card = _cardField.text;
    NSString *date = _datePickerField.text;
    NSString *begin = _beginTimePickerField.text;
    NSString *end = _endTimePickerField.text;
    if (!name || name.length == 0) {
        [self showAlert:@"请输入姓名."];
        [_nameField becomeFirstResponder];
        return;
    }
    if (!phone || phone.length == 0) {
        [self showAlert:@"请输入电话号码."];
        [_phoneField becomeFirstResponder];
        return;
    }
    if (!card || card.length == 0) {
        [self showAlert:@"请输入身份证号码."];
        [_cardField becomeFirstResponder];
        return;
    }
    if (!date || date.length == 0) {
        [self showAlert:@"请选择预约日期."];
        return;
    }
    if (!begin || begin.length == 0) {
        [self showAlert:@"请选择开始时间点"];
        return;
    }
    if (!end || end.length == 0) {
        [self showAlert:@"请选择结束时间点."];
        return;
    }
    //调用提交
    [_dataService commitReservationWithName:name Phone:phone CardNo:card Date:date BeginTime:begin EndTime:end ListItem:_listItem];
    [self startLoadingViewWithText:@"正在提交"];
}

-(void)showAlert:(NSString *) msg
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

#pragma mark - Text Fields
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == _nameField || textField == _phoneField || textField == _cardField) {
        [_datePicker hideDatePicker];
        return YES;
    }
    if (textField == _datePickerField) {
        [self.view endEditing:YES];
        NSLog(@"选择日期");
        [_datePicker initPickerWithMode:UIDatePickerModeDate DoneBlock:^(NSDate *result) {
            NSLog(@"选择了日期%@", result);
            _pickedDate = result;
            NSDateFormatter *df = [NSDateFormatter new];
            [df setDateFormat:@"yyyy-MM-dd"];
            _datePickerField.text = [df stringFromDate:result];
        } BeginDate:[NSDate date] EndDate:nil];
        [_datePicker showDatePicker];
        return NO;
    }
    if (textField == _beginTimePickerField) {
        [self.view endEditing:YES];
        NSLog(@"选择开始时间");
        NSDate *begin = nil;
        NSDateFormatter *df = [NSDateFormatter new];
        [df setDateFormat:@"yyyy-MM-dd"];
        if ([[df stringFromDate:_pickedDate] isEqualToString:[df stringFromDate:[NSDate date]]]) {
            //选择的是今天,只能从当前时段开始选时间
            begin = _pickedDate;
        } else {
            //选择的不是今天,可以选任意时段
            begin = nil;
        }
        [_datePicker initPickerWithMode:UIDatePickerModeTime DoneBlock:^(NSDate *result) {
            NSLog(@"选择了日期%@", result);
            _pickedBeginTime = result;
            NSDateFormatter *df = [NSDateFormatter new];
            [df setDateFormat:@"HH:mm"];
            _beginTimePickerField.text = [df stringFromDate:result];
        } BeginDate:begin EndDate:nil];
        [_datePicker showDatePicker];
        return NO;
    }
    if (textField == _endTimePickerField) {
        [self.view endEditing:YES];
        NSLog(@"选择结束时间");
        [_datePicker initPickerWithMode:UIDatePickerModeTime DoneBlock:^(NSDate *result) {
            NSLog(@"选择了日期%@", result);
            NSDateFormatter *df = [NSDateFormatter new];
            [df setDateFormat:@"HH:mm"];
            _endTimePickerField.text = [df stringFromDate:result];
        } BeginDate:_pickedBeginTime EndDate:nil];
        [_datePicker showDatePicker];
        return NO;
    }
    return YES;
}

//开始输入的时候向上移动整个界面，这个输入框不会被挡住
-(void)textFieldDidBeginEditing:(UITextView *)textView
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.2f];
    CGRect frame = self.view.frame;
    frame.origin.y = frame.origin.y - INPUT_VIEW_OFFSET * textView.tag;
    self.view.frame = frame;
    [_scrollView scrollRectToVisible:CGRectMake(0, CGRectGetMinY(textView.frame), 320, 200) animated:NO];
    [UIView commitAnimations];
}

//输入结束的时候把整个界面向下移动，这个能恢复正常
-(void)textFieldDidEndEditing:(UITextView *)textView
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.2f];
    CGRect frame = self.view.frame;
    frame.origin.y = frame.origin.y + INPUT_VIEW_OFFSET * textView.tag;
    self.view.frame = frame;
    [UIView commitAnimations];
}

#pragma mark - Data Service
-(void)loadDataFinished
{
    [self stopLoadingView];
    //提交成功之后返回
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"错误" message:@"提交预约信息失败." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
@end
